<script setup>
import {ref} from "vue";
import request  from '@/api/request'

let emits=defineEmits(["ref-table"])
let props=defineProps(["data"])
let orderReturns=ref({...props.data})
let shipmentHistory=ref([])

let orderReturnDetails=ref([])
orderReturnDetails.value=orderReturns.value.orderReturnDetailsList
let warehouse=ref([])
let User=ref([])

let isYes=ref("false")
let onSubmit=()=>{
        request({
            url:"/orderRreturns/"+isYes.value,
            method:"put",
            data:orderReturns.value
        }).then(res=>{  
        })
        request({
            url:"/orderReturnDetails",
            method:"put",
            data:orderReturnDetails.value
        }).then(res=>{
            emits("ref-table")
        })
}

let page=ref({
    current:1,
    size:10000,
    total:0,
    records:[]
})

let location=()=>{
    request({
        //查询员工
            url:"/system/user/list",
            method:"post",
            data:{
                current:1,
                size:1000
            }
        }).then(res=>{
            User.value=res.data.records
        })
    request({
        //查询仓库
        url:"/warehouse",
        method:"get",
        params:{
            current:page.value.current,
            size:page.value.size
    }
    }).then(res=>{
        warehouse.value=res.data.date
    })

    request({
        //查询
        url:"ShipmentHistory",
        method:"get",
        params:{
            current:page.value.current,
            size:page.value.size
    }
    }).then(res=>{
        shipmentHistory.value=res.data.records
    })
}
location();

//点击发货订单查询发货详情
let a=()=>{
    request({
        //查询客户
        url:"/orderReturnDetails/select",
        method:"get",
        params:{
            orderReturnDetailOrderReturnsId:orderReturns.value.orderReturnsId
    }
    }).then(res=>{
        console.log(res.data);
        for(let i =0;i<res.data.length;i++){
            orderReturnDetails.value[i].shippingDetailsWeight=res.data[i].material.materialWeight
            orderReturnDetails.value[i].quoteDetailsActualSellingPrice=res.data[i].quoteDetails.quoteDetailsActualSellingPrice
        }
        
    })
}
a();

//计算
let jisuan=()=>{
    for(let i = 0;i<orderReturnDetails.value.length;i++){
        orderReturnDetails.value[i].orderReturnDetailOutboundReturnWeight=orderReturnDetails.value[i].orderReturnDetailOutboundReturns*orderReturnDetails.value[i].shippingDetailsWeight;
        orderReturnDetails.value[i].orderReturnDetailNoTheTotalSalePrice=orderReturnDetails.value[i].orderReturnDetailOutboundReturns*orderReturnDetails.value[i].quoteDetailsActualSellingPrice
    }
}
let jisuan1=()=>{
    for(let i = 0;i<orderReturnDetails.value.length;i++){
        orderReturnDetails.value[i].orderReturnDetailNoOutboundReturnWeight=orderReturnDetails.value[i].orderReturnDetailNoOutboundReturns*orderReturnDetails.value[i].shippingDetailsWeight;
        orderReturnDetails.value[i].orderReturnDetailTheTotalSalePrice=orderReturnDetails.value[i].orderReturnDetailNoOutboundReturns*orderReturnDetails.value[i].quoteDetailsActualSellingPrice;
    }
}
let shuliang=()=>{
    jisuan();
    jisuan1();
    let orderReturnsOutboundQuantity=0;
    let orderReturnsOutboundWeight=0;
    let orderReturnsTheOutboundAmount=0
    let orderReturnsTotalQuantity=0
    let orderReturnsTotalWeight=0
    for(let i = 0;i<orderReturnDetails.value.length;i++){
        orderReturnsOutboundQuantity+=orderReturnDetails.value[i].orderReturnDetailOutboundReturns
        orderReturnsOutboundWeight+=orderReturnDetails.value[i].orderReturnDetailOutboundReturnWeight
        orderReturnsTheOutboundAmount+=orderReturnDetails.value[i].orderReturnDetailNoTheTotalSalePrice
        orderReturnsTotalQuantity+=(orderReturnDetails.value[i].orderReturnDetailOutboundReturns+orderReturnDetails.value[i].orderReturnDetailNoOutboundReturns)
        orderReturnsTotalWeight+=orderReturnDetails.value[i].orderReturnDetailOutboundReturnWeight+orderReturnDetails.value[i].orderReturnDetailNoOutboundReturnWeight
    }
    orderReturns.value.orderReturnsOutboundQuantity=orderReturnsOutboundQuantity
    orderReturns.value.orderReturnsOutboundWeight=orderReturnsOutboundWeight.toFixed(2)
    orderReturns.value.orderReturnsTheOutboundAmount=orderReturnsTheOutboundAmount.toFixed(2)
    orderReturns.value.orderReturnsTotalQuantity=orderReturnsTotalQuantity
    orderReturns.value.orderReturnsTotalWeight=orderReturnsTotalWeight.toFixed(2)
}
</script>
<template>
    <div>
        <el-card>
            <template #header>
                <el-form :model="orderReturns" ref="form" :inline="false" class="card_wrap">
                    <el-form-item label="订单号："  prop="orderReturnsQuotationId" class="c_item">
                            <el-select placeholder="发货订单号" v-model="orderReturns.orderReturnsQuotationId" style="width: 150px;"disabled>
                                <el-option v-for="item in shipmentHistory" :label="item.shipmentHistoryId" :value="item.shipmentHistoryId">
                                </el-option>
                            </el-select>
                    </el-form-item>
                    <el-form-item label="退货时间：" prop="orderReturnsApplicationTimeline" class="c_item">
                            <el-date-picker v-model="orderReturns.orderReturnsApplicationTimeline" type="date" value-format="YYYY-MM-DDs" placeholder="选择日期时间">
                            </el-date-picker>
                    </el-form-item>
                    <el-form-item label="退货原因：" prop="orderReturnsCause" class="c_item">
                            <el-input v-model="orderReturns.orderReturnsCause" style="width: 100%;"></el-input>
                    </el-form-item>
                    <el-form-item label="退货仓库" prop="orderReturnsWarehouse" class="c_item">
                        <el-select  placeholder="仓库" v-model="orderReturns.orderReturnsWarehouse" @change="location" style="width:150px;">
                            <el-option label="全部" value="全部">
                                </el-option>
                            <el-option v-for="item in warehouse" :label="item.warehouseName" :value="item.warehouseId">
                                </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="已出库退货数量：" prop="orderReturnsOutboundQuantity" class="c_item">
                            <el-input v-model="orderReturns.orderReturnsOutboundQuantity" disabled style="width: 150px;"></el-input>
                    </el-form-item>
                    <el-form-item label="已出库退货重量：" prop="orderReturnsOutboundWeight" class="c_item">
                            <el-input v-model="orderReturns.orderReturnsOutboundWeight" disabled style="width: 150px;"></el-input>
                    </el-form-item>
                    <el-form-item label="已出库退货总金额：" prop="orderReturnsTheOutboundAmount" class="c_item">
                            <el-input v-model="orderReturns.orderReturnsTheOutboundAmount" disabled style="width: 150px;"></el-input>
                    </el-form-item>
                    <el-form-item label="退货总数量：" prop="orderReturnsTotalQuantity" class="c_item">
                            <el-input v-model="orderReturns.orderReturnsTotalQuantity" disabled style="width: 150px;"></el-input>
                    </el-form-item>
                    <el-form-item label="退货总重量：" prop="orderReturnsTotalWeight" class="c_item">
                            <el-input v-model="orderReturns.orderReturnsTotalWeight" disabled style="width: 150px;"></el-input>
                    </el-form-item>
                    <el-form-item label="经办人：" prop="orderReturnsAttn" class="c_item">
                            <el-select v-model="orderReturns.orderReturnsAttn" style="width: 150px;">
                                <el-option v-for="item in User" :label="item.nickname" :value="item.id+''">
                                </el-option>
                            </el-select>
                    </el-form-item>

                    <el-form-item label="是否发货：" prop="isYes" class="c_item">
                            <el-radio-group v-model="isYes">
                                <el-radio value="true">是</el-radio>
                                <el-radio value="false">否</el-radio>
                            </el-radio-group>
                    </el-form-item>

                </el-form>

                <el-table :data="orderReturnDetails" border stripe>
                    <el-table-column prop="orderReturnDetailProductsId" label="产品编码" width="100px;"/>
                    <el-table-column prop="orderReturnDetailQuoteDetailsId" label="报价单明细编码" width="130px;"/>
                    <el-table-column prop="orderReturnDetailShippingDetailsId" label="发货明细编码" width="110px;"/>
                    <el-table-column prop="orderReturnDetailOutboundReturns" label="已出库退货数量" width="180px;" >
                        <template #default="scope">
                            <el-input-number v-model="scope.row.orderReturnDetailOutboundReturns" :min="0" :max=scope.row.shippingDetailsOutboundShipments  :step="1" step-strictly @blur="shuliang" @change="shuliang"></el-input-number>
                        </template>
                    </el-table-column>
                    <el-table-column prop="orderReturnDetailOutboundReturnWeight"  label="出库退货重量" width="130px;">
                     <template #default="scope">
                            <el-input v-model="scope.row.orderReturnDetailOutboundReturnWeight" disabled></el-input>
                        </template>
                    </el-table-column>
                    <el-table-column prop="orderReturnDetailNoTheTotalSalePrice"  label="已出库总价" width="130px;">
                     <template #default="scope">
                            <el-input v-model="scope.row.orderReturnDetailNoTheTotalSalePrice" disabled></el-input>
                        </template>
                    </el-table-column>
                    <el-table-column prop="orderReturnDetailNoOutboundReturns"  label="未出库退货数量" width="180px;">
                     <template #default="scope">
                            <el-input-number v-model="scope.row.orderReturnDetailNoOutboundReturns" :min="0" :max=scope.row.shippingDetailsToBeShipped-scope.row.shippingDetailsOutboundShipments @blur="shuliang" @change="shuliang" :step="1" step-strictly></el-input-number>
                        </template>
                    </el-table-column>
                    <el-table-column prop="orderReturnDetailNoOutboundReturnWeight"  label="未出库退货重量" width="130px;">
                     <template #default="scope">
                            <el-input v-model="scope.row.orderReturnDetailNoOutboundReturnWeight" disabled></el-input>
                        </template>
                    </el-table-column>
                    <el-table-column prop="orderReturnDetailTheTotalSalePrice"  label="未出库退货总价" width="130px;">
                     <template #default="scope">
                            <el-input v-model="scope.row.orderReturnDetailTheTotalSalePrice" disabled></el-input>
                        </template>
                    </el-table-column>
                </el-table>
                
                
                
                <el-form-item>
                    <el-button type="primary" @click="onSubmit">立即修改</el-button>
                </el-form-item>
            </template>
        </el-card>
    </div>
</template>
<style scoped>

.card_wrap {
    width: 98%;
    display: flex;
    flex-wrap: wrap;
    justify-content:flex-start;
}
.c_item{
    flex:1;
    margin:0px 5px 15px 0;
    width: calc((100%-0px)/2); 
    min-width:calc((100% - 10px)/2);
    max-width:calc((100% - 10px)/2);
}
</style>